package
{
	import flash.geom.Point;
	public class CTouchInterfaceManager
	{
		public static	var tp_pos:Point = new Point();
		public static	var tp_type:int = 0;
		public static	var tp_old_pos:Point = new Point();
		public static	var tp_old_type:int = 0;
		
		public static var TP_TOUCH_OFF:int = 0;     // Not being touched
		public static var TP_TOUCH_ON:int = 1;     // Being touched
		
		protected var m_currentElements:Array = new Array();
		private var m_nbElements:int;
		private var m_elementsWithCapture:InterfButton = null;
		public function CTouchInterfaceManager()
		{
			m_nbElements = 0;
		}
		
		public function RegisterElement(element:InterfButton):void
		{
			if (element.m_touchRegistered == false)
			{
				element.m_touchRegistered = true;
				m_currentElements[m_nbElements++] = element;
			}
		}
		
		public function RemoveElement(element:InterfButton):void
		{
			if (element.m_touchRegistered == true)
			{
				element.m_touchRegistered = false;
				
				
				for (var i:int = 0; i < m_nbElements; i++)
				{
					if (m_currentElements[i] == element)
					{
						for (var j:int = i; j < m_nbElements - 1; j++)
						{
							m_currentElements[j] = m_currentElements[j + 1];
						}
						
						m_nbElements--;
						
						return;
					}
				}
			}
		}
		
		
		public function ClearAll():void
		{
			for (var i:int = 0; i < m_nbElements; i++)
			{
				m_currentElements[i].m_touchRegistered = false;
				m_currentElements[i] = null;
			}
			m_nbElements = 0;
		}
		
		public function Render():void
		{
			var g:MyGraphics = Game.GetMyGraphics();
			for(var i:int = 0; i< m_nbElements;i++)
			{
				if(m_currentElements[i].IsEnable())
				{
					g.DrawRect(m_currentElements[i].posL.x,
						m_currentElements[i].posL.y,
						m_currentElements[i].posR.x - m_currentElements[i].posL.x,
						m_currentElements[i].posR.y - m_currentElements[i].posL.y,
						0xff0000,1);				
				}
			}
		}
		public function Update():void
		{
			if (m_nbElements < 1)
			{
				return;
			}

			var crtElement:InterfButton  = null;
			if(tp_type == TP_TOUCH_ON)
			{
				if(tp_old_type == TP_TOUCH_ON)
				{
					crtElement = SearchElementWithFocus();
					
					if (crtElement != null)
					{
						
					}
					else
					{
						crtElement = SearchElementForFocus(tp_pos.x, tp_pos.y, true);
						
						if (crtElement != null)
						{
							SetElementTouchOn(crtElement, tp_pos.x, tp_pos.y);
							crtElement.SetFocus(true);
							m_elementsWithCapture = crtElement;
						}
					}
				}
				else
				{
					crtElement = SearchElementForFocus(tp_pos.x, tp_pos.y, true);
					
					if (crtElement != null)
					{
						SetElementTouchOn(crtElement, tp_pos.x, tp_pos.y);
						crtElement.SetFocus(true);
						m_elementsWithCapture = crtElement;
					}
				}
			}
			else
			{
				if(tp_old_type == TP_TOUCH_ON)
				{
					if(m_elementsWithCapture!=null)
					{
						var isOverCluster:Boolean = false;
						if (m_elementsWithCapture.HitTest(tp_pos.x, tp_pos.y))					
						{
							isOverCluster = true;
						}
						SetElementTouchOff(m_elementsWithCapture, tp_pos.x, tp_pos.y, isOverCluster);
						m_elementsWithCapture.SetFocus(false);
						m_elementsWithCapture = null;
						//crtElement = SearchElementWithFocus();
						//if (crtElement != null)
						//{
							//crtElement.SetFocus(false);
						//}
					}
				}
			}
		}
		
		private function SetElementTouchOff(element:InterfButton,  mX:int,  mY:int, inside:Boolean):void
		{
			element.OnTouchUp(mX, mY, inside);											
		}
		
		private function SetElementTouchOn(element:InterfButton,  mX:int,  mY:int):void
		{
			element.OnTouchDown(mX, mY);											
		}
		
		public function SearchElementWithFocus():InterfButton
		{
			for (var i:int = 0; i < m_nbElements; i++)
			{
				if (m_currentElements[i].HasFocus())
				{
					return m_currentElements[i];
				}
			}
			
			return null;
		}
		
		public function SearchElementForFocus( mX:int, mY:int, isDrag:Boolean):InterfButton
		{
			for (var i:int = 0; i < m_nbElements; i++)
			{
				if(m_currentElements[i].IsEnable())
				{
					if (m_currentElements[i].HitTest(mX, mY))					
					{
						return m_currentElements[i];
					}
				}
			}			
			return null;
		}
	}
}